﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.8.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-cn.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<script src="js/xing.js" type="text/javascript"></script>
<script type="text/javascript" src="js/WdatePicker.js"></script>
<script type="text/javascript">
	$.jgrid.no_legacy_api = true;
	$.jgrid.useJSON = true;
</script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
var myWaiters = [];
var fetching = false;

function getOrderTime() {
	WdatePicker({dateFmt:'yyyy-MM-dd 日 HH点', minDate:'%y-%M-{%d}'});
};

function searchTask() {
	var company = $.trim($("#sf_company").val());
	var state = $("#sf_state").val();
	var sdate = $.trim($("#sf_startdate").val());
	var edate = $.trim($("#sf_enddate").val());
	
	if ((sdate.length + edate.length) == 10) {
		alert("必须同时选择起始时间和结束时间！");
		return;
	}
	
	var q = "action=3&comid=" + company + "&state=" + state;
	if ((sdate.length == 10) && (edate.length == 10)) {
		q += "&rectime=[" + sdate + " 00:00:00," + edate + " 23:59:59]";
	}
	jQuery("#list1").jqGrid('setGridParam',{page:1,url:"main?" + q}).trigger("reloadGrid");
};

function getCustByCID() {
  if (fetching) return;
  
  var cid = $.trim($("#cid").val());
	if (cid == "")
		return;
		
	$.get("main",{"action":"4","oper":"query","cid":cid},function (data, textStatus) {
		if (data.code != 0)
			return;
			
		fetching = true;
		$("#cname").val(data.cname);
		$("#csex").val(data.csex);
		$("#ctel").val(data.ctel);
		$("#cmobile").val(data.cmobile);
		$("#caddress").val(data.caddress);
		$('#ctype').val(data.ctype);
		fetching = false;
	});
};

function getCustByTel() {
	if (fetching) return;
	
	var v = "";

	if (this.id == "cmobile")
		v = $.trim($("#cmobile").val());
	else
		v = $.trim($("#ctel").val());
		
	$.get("main",{"action":"24","tel":v},function (data, textStatus) {
		if (data.code != 0)
			return;
		
		fetching = true;
		$("#cid").val(data.cid);
		$("#cname").val(data.cname);
		$("#csex").val(data.csex);
		$("#ctel").val(data.ctel);
		$("#cmobile").val(data.cmobile);
		$("#caddress").val(data.caddress);
		$('#ctype').val(data.ctype);
		$('#note').val(data.remark);
		$('#note').fadeTo(2000, 0.3);
		fetching = false;
	});
}

function initWaiters() {
	$.get('main?action=11',function (data, textStatus) {
		$.extend(true, myWaiters, data);
	});
};

function initCompany() {
	$.get('main?action=14',function (data, textStatus) {
		var ops = data;
		var len = ops.length;
		$("#sf_company").html("<option value='0'>全  部</option>" + ops.substring(8, len - 9));
	});
};

function companyChange() {
	var comid = $("#comid").val();
	var sp = initWaiter(comid);
	if (sp == "") sp = "<option value='0'>选择服务人员</option>";
	$("#sname").html(sp);
};

function initWaiter(comid) {
	var ops = "";
	
	for (i = 0; i < myWaiters.length; i++) {
		if (myWaiters[i].comid == comid) {
			for (j = i; j < myWaiters.length; j++) {
				if (myWaiters[j].comid != comid)
					return ops;
				ops += "<option value='" + myWaiters[j].wname + ":" + myWaiters[j].mobile + "'>" + myWaiters[j].wname + ":" + myWaiters[j].mobile + "</option>";
			}
			return ops;
		}
	}
	
	return ops;
};

function checkValue(postdata, formid) {
	var note = postdata.note;
	var caddress = postdata.caddress;
	if (note.length > 40) {
		return [false,"服务内容的长度不能超过40"];
	}
	
	if (caddress.length > 40) {
		return [false,"住址的长度不能超过40"];
	}
	
	return [true,""];
};

function setGrid() {
	jQuery("#list1").jqGrid({ 
			url:'main?action=3',
			datatype: "json",
			hidegrid: "false",
			colNames:['任务序号','客户编号','客户姓名','性别','人员类型','服务类型','服务内容','预约服务时间','手机号码','座机','住址','登记时间','服务公司','服务人员','服务人员手机','分派时间','开始时间','结束时间','状态','录入人','派单人'], 
			colModel:[ 
					{name:'taskid', width:50, sortable:false,align:"right",editable:true,editoptions:{readonly:true,size:10},frozen:true},
					{name:'cid', width:80, sortable:false,align:"right",editable:true,editoptions:{size:20,maxlength:13},editrules:{required:true},frozen:true},
					{name:'cname', width:50, sortable:false, align:"right",editable:true,editoptions:{size:8,maxlength:4},editrules:{required:true},frozen:true},
					{name:'csex',width:30, sortable:false,align:"right", editable:true,edittype:"select", formatter:'select', editoptions:{value:"F:女;M:男"}},
					{name:'ctype', width:60, sortable:false, align:"right",editable:true,editoptions:{size:8,maxlength:8},editrules:{edithidden:true},hidden:true},
					{name:'cservice',width:60, sortable:false,align:"right", editable:true,edittype:"select", formatter:'select',editoptions:{value:"0:新居开荒;1:家居保洁;2:单位保洁;3:管道疏通;4:家电维修;5:电脑维修;6:装饰装修;7:家政服务;8:家教服务;9:票务服务;10:配送服务;11:搬家服务;12:开锁服务;13:房屋维修;14:其他"}},
					{name:'note',width:80, sortable:false,align:"right",editable:true,edittype:"textarea",editoptions:{rows:"2",cols:"18"}},
					{name:'ordertime', width:100, sortable:false, align:"right",editable:true,editoptions:{size:20,maxlength:16}},
					{name:'cmobile', width:80, sortable:false,align:"right", editable: true, editoptions:{size:15,maxlength:11}},
					{name:'ctel', width:70, sortable:false,align:"right", editable: true, editoptions:{size:15,maxlength:11}},
					{name:'caddress',width:160, sortable:false,align:"right",editable:true,edittype:"textarea",editoptions:{rows:"2",cols:"18"}},
					{name:'rectime', width:120, sortable:false,align:"right"},
					{name:'comid', width:60, sortable:false, align:"right",hidden:true,editable:true,editrules:{edithidden:true},edittype:'select',editoptions:{dataUrl:'main?action=14'}},
					{name:'sname', width:60, sortable:false, align:"right",editable:true,edittype:'select', editoptions:{value:"0:选择服务人员"}},
					{name:'smobile', width:80, sortable:false,align:"right"},
					{name:'assigntime', width:120, sortable:false,align:"right"},
					{name:'picktime', width:120, sortable:false,align:"right"},
					{name:'finishtime', width:120, sortable:false,align:"right"},
					{name:'state',width:50, sortable:false,align:"right", edittype:"select", formatter:'select', editoptions:{value:"0:未分派;1:已分派;2:已开始;3:已完成;4:已冲正;5:已回访"}},
					{name:'recorder', width:60, sortable:false,align:"right"},
					{name:'assigner', width:60, sortable:false,align:"right"}
			], 
			viewrecords: true, 
			rowNum:20,
			pager: '#gridpager1',
			height: 480,
			autowidth: true,
			shrinkToFit:false,
			editurl:'main?action=3',
			caption:"分派任务管理"
	}).jqGrid('setFrozenColumns')};
	
	
	$(document).ready(function(){
		initWaiters();
		setGrid();
		jQuery("#list1").jqGrid('navGrid','#gridpager1',{edit:false,add:false,del:false,refresh:false,search:false})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"添加", 
   		buttonicon:"ui-icon-plus", 
   		onClickButton: function(){
   			jQuery("#list1").jqGrid('editGridRow','new',
													{addCaption:"添加一条任务",
														bSubmit:"保存",bCancel:"取消",modal: true, 
														closeAfterAdd:true,	reloadAfterSubmit:true,
														beforeSubmit:checkValue,
														beforeShowForm:function(formid){
															$('#tr_taskid', formid).hide();
															$('#tr_cid', formid).show(); 
															$('#tr_csex', formid).show(); 
															$('#tr_ctype', formid).hide(); 
															$('#tr_cmobile', formid).show();
															$('#tr_ctel', formid).show();
															$('#tr_comid', formid).hide();
															$('#tr_sname', formid).hide();
															$('#cname').attr("readonly",false);
															$('#cservice').attr("disabled",false);
															$('#note').attr("readonly",false);
															$('#caddress').attr("readonly",false);
														}
													}); 
					}
				})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"更改", 
   		buttonicon:"ui-icon-wrench", 
   		onClickButton: function(){
   			var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
			if( gr != null )
				jQuery("#list1").jqGrid('editGridRow',gr,
													{editCaption:"分配任务",
														bSubmit:"保存",bCancel:"取消",modal: true,
														closeAfterEdit:true,
														viewPagerButtons:false,	
														reloadAfterSubmit:true,
														beforeShowForm:function(formid) {
															$('#tr_taskid', formid).hide();
															$('#tr_cid', formid).hide(); 
															$('#tr_csex', formid).hide(); 
															$('#tr_ctype', formid).hide();
															$('#tr_cmobile', formid).hide();
															$('#tr_ctel', formid).hide();
															$('#tr_comid', formid).show();
															$('#tr_sname', formid).show();
															$('#tr_state', formid).show();
															$('#cname').attr("readonly",true);
															$('#cservice').attr("disabled",true);
															$('#note').attr("readonly",true);
															$('#caddress').attr("readonly",true);
														},
														afterShowForm : function (formid) {
															setTimeout(companyChange, 100);
														}
													});
				else
					showTips("请选择待分派的任务");
				}
			})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"删除", 
   		buttonicon:"ui-icon-close", 
   		onClickButton: function(){
   			var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
			if( gr != null )
												  jQuery("#list1").jqGrid('delGridRow', gr, {
												  	caption:"删除确认", 
												  	top: 200, left: 400,
												  	msg:"确定要删除这个任务吗?",
												  	bSubmit:"删除",bCancel:"取消",modal: true,
												  	closeAfterEdit:true, 
												  	reloadAfterSubmit:true,
												  	afterSubmit : function(r, postdata) {
													  					var data = eval('(' + r.responseText + ')');
													  					if (data.code != 0)
													  						return [false, data.message];
													  					else
													  						return [true, data.message];
													  }
													});
			else
				showTips("请选择要删除的任务");
			}
		})
		.jqGrid('navButtonAdd','#gridpager1', {
			caption: "回访",
			buttonicon:"ui-icon-refresh",
			onClickButton: function() {
				var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
				if (gr != null) {
					var state = jQuery("#list1").jqGrid('getCell', gr, 18);
					if (state != "3") {
						alert("任务尚未完成不能回访！");
						return;
					}
					var taskid = jQuery("#list1").jqGrid('getCell', gr, 0);
					var cname = jQuery("#list1").jqGrid('getCell', gr, 2);
					var mobile = jQuery("#list1").jqGrid('getCell', gr, 8);
					var tel = jQuery("#list1").jqGrid('getCell', gr, 9);
					if (confirm("回访成功请点 [确定] 按钮\n\n客户：" + cname + "\n手机：" + mobile + "\n座机：" + tel) == true) {
					  $.get("main",{action:"3",oper:"end",taskid:taskid},function (data, textStatus) {
							if (data.code == 1) 
								jQuery("#list1").jqGrid().trigger('reloadGrid');
							else
								alert(textStatus);
						});
					}
				} else
					showTips("请选择要回访的任务");
				
			}
		});
   		
		$("#sf_submit").click(searchTask);
		$("#comid").live('change', companyChange);
		$("#ordertime").live('click', getOrderTime);
		//$("#cid").live('blur', getCustByCID);
		$("#cmobile").live('blur', getCustByTel);
		$("#ctel").live('blur', getCustByTel);
		initCompany();
	});
</script>

<body style="background-color:#4e84c0;">

											<form>
											<table>
											<tr>
												<td> 工单状态</td>
												<td width="100"><select id="sf_state" name="sf_state">
																			<option value='9'>全  部</option>
																			<option value='0'>未分派</option>
																			<option value='1'>已分派</option>
																			<option value='2'>已开始</option>
																			<option value='3'>已完成</option>
																			<option value='4'>已冲正</option>
																			<option value='5'>已回访</option>
																		</select></td>
												<td> 服务公司</td>
												<td width="120"><select id="sf_company" name="sf_company"><option value='0'>全  部</option></select></td>
												<td>起始登记时间</td>
												<td><input id="sf_startdate" name="sf_recdate" type="text" text="" size=10 onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})" /></td>
												<td>结束登记时间</td>
												<td><input id="sf_enddate" name="sf_enddate" type="text" text="" size=10 onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})" /></td>
												<td width="100" align="right"><input type="button" id="sf_submit" value="查 询"></td>
											</tr>
											</table>
											</form>
									
					<div id="RightPane" >
						<div class="innertube">
							<table id="list1"></table>
							<div id="gridpager1"></div>
						</div>
					</div>

</body>
